package com.wxzz.elearing.comment.api.app;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.comment.dto.*;
import com.wxzz.elearing.comment.entity.CommunityComment;
import com.wxzz.elearing.comment.entity.StudyComment;
import com.wxzz.elearing.comment.service.ICommunityCommentService;
import com.wxzz.elearing.comment.service.IStudyCommentService;
import com.wxzz.elearing.comment.vo.CommunityAnswersVo;
import com.wxzz.elearing.comment.vo.CommunityQuestionsContentDescVo;
import com.wxzz.elearing.comment.vo.StudyAnswersVo;
import com.wxzz.elearing.comment.vo.StudyQuestionsContentDescVo;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/wx-study-comment-community")
@Api(tags = "学以致用评论接口(小程序)")
public class WxStudyCommentController {

    @Resource
    private IStudyCommentService iStudyCommentService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @ApiOperation(value = "学以致用评论(小程序)", notes = "学以致用评论(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveComment", method = RequestMethod.POST)
    public ReturnVO saveComment(@RequestBody StudyComment comment, @RequestHeader(value = "token") String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        comment.setIcon(jsonObject.getString("icon"));
        comment.setNickname(jsonObject.getString("nickname"));
        comment.setWxUserId(jsonObject.getLong("id"));
        return iStudyCommentService.saveComment(comment);
    }

    @ApiOperation(value = "学以致用根据文章id获取详情(小程序)", notes = "学以致用根据文章id获取详情(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listAllComment/{articleId}", method = RequestMethod.POST)
    public ReturnVO<StudyQuestionsContentDescVo> listAllComment(@PathVariable Long articleId) {
        return iStudyCommentService.listAllComment(articleId);
    }

    @ApiOperation(value = "学以致用根据文章id列出所有评论", notes = "学以致用根据文章id列出所有评论", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "answersDescList", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<StudyComment>>> answersDescList(@RequestBody StudyAnswersDescDto answersDescDto) {
        return iStudyCommentService.answersDescList(answersDescDto);
    }

    @ApiOperation(value = "学以致用查询个人所有评论", notes = "学以致用查询个人所有评论", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "answersList", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<StudyAnswersVo>>> getAnswersList(@RequestBody  StudyAnswersDto answersDto) {
        return iStudyCommentService.getAnswersList(answersDto);
    }

}
